$.fn.serializeObject = function()
{
    let o = {};
    let a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};


let testEditor;
$(function() {
    testEditor = editormd("sgl_editor", {
        placeholder:"请开始你的表演！",
        width : "100%",
        height  : 900,
        syncScrolling : "single",
        path    : "/lib/lib/", //依赖lib文件夹路径
        emoji : true,
        screen: false,
        taskList : true,
        tocm     : true,
        imageUpload: true,
        imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
        imageUploadURL: "imageUpload"		//上传图片控制器Mapping
    });


    $("#saveBtn").on('click', function () {
        let url = window.location.href;
        let index = url.indexOf("update");
        let article;

        let forms = $("#form").serialize();
        if (forms.toString().split('&').length == 6) {
            article = $("#form").serializeObject();
            article.articleState = 0;
            if (index > 0) {
                article.articleId = url.substring(url.lastIndexOf("/") + 1, url.length);
                $.ajax({
                    //请求方式
                    type: "PUT",
                    contentType: "application/json;charset=UTF-8",
                    //请求地址
                    url: "/blog/articleSave",
                    data : JSON.stringify(article),
                    async: true,
                    //请求成功
                    dataType:"json",
                    success: function (data) {
                        if (data.code == 0) {
                            showNotification("top", "提示", "修改成功！即将跳转", "info")
                            setTimeout(function () {
                                window.location.href = "/blog/myBlog"
                            },1000)
                        }else {
                            showNotification("top", "提示", "修改失败！", "warning")
                        }
                    },
                    //请求失败，包含具体的错误信息
                    error: function () {
                        showNotification("top", "提示", "修改失败！", "danger")
                    }
                });
            }else {
                $.ajax({
                    //请求方式
                    type: "POST",
                    contentType: "application/json;charset=UTF-8",
                    //请求地址
                    url: "/blog/articleSave",
                    data : JSON.stringify(article),
                    async: true,
                    //请求成功
                    dataType:"json",
                    success: function (data) {
                        if (data.code == 0) {
                            showNotification("top", "提示", "保存成功！即将跳转", "info")
                            setTimeout(function () {
                                window.location.href = "/blog/myBlog"
                            },1000)
                        }else {
                            showNotification("top", "提示", "修改失败成功！", "info")
                        }

                    },
                    //请求失败，包含具体的错误信息
                    error: function () {
                        showNotification("top", "提示", "保存失败！", "danger")
                    }
                });
            }
        } else {
            showNotification("top", "提示", "请填写必填信息！", "warning")
        }
    })
});